﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;
using System.Collections;

namespace DALL.Progect
{
    public class T_PUB_PROJECT
    {
        public T_PUB_PROJECT()
        { }

        /// <summary>
        /// 添加项目
        /// </summary>
        /// <param name="model"></param>
        /// <param name="matralList"></param>
        public void AddProgect(Model.Progect.T_PUB_PROJECT model, IList<Model.Progect.T_PUB_PROJECTMATERIAL> matralList)
        {
            ArrayList strArry = new ArrayList();

            #region 添加项目表

            StringBuilder strSql = new StringBuilder();
            StringBuilder strSql1 = new StringBuilder();
            StringBuilder strSql2 = new StringBuilder();
            if (model.PROJECT_ID != null)
            {
                strSql1.Append("PROJECT_ID,");
                strSql2.Append("'" + model.PROJECT_ID + "',");
            }
            if (model.BRANCH_ID != null)
            {
                strSql1.Append("BRANCH_ID,");
                strSql2.Append("" + model.BRANCH_ID + ",");
            }
            if (model.PROJECTTYPE_ID != null)
            {
                strSql1.Append("PROJECTTYPE_ID,");
                strSql2.Append("" + model.PROJECTTYPE_ID + ",");
            }
            if (model.PROJECT_NAME != null)
            {
                strSql1.Append("PROJECT_NAME,");
                strSql2.Append("'" + model.PROJECT_NAME + "',");
            }
            if (model.PRICE != null)
            {
                strSql1.Append("PRICE,");
                strSql2.Append("" + model.PRICE + ",");
            }
            if (model.TIME != null)
            {
                strSql1.Append("TIME,");
                strSql2.Append("'" + model.TIME + "',");
            }
            if (model.REMARK != null)
            {
                strSql1.Append("REMARK,");
                strSql2.Append("'" + model.REMARK + "',");
            }
            if (model.FLAG != null)
            {
                strSql1.Append("FLAG,");
                strSql2.Append("'" + model.FLAG + "',");
            }
            if (model.BUDGET != null)
            {
                strSql1.Append("BUDGET,");
                strSql2.Append("'" + model.BUDGET + "',");
            }
            if (model.Count != null)
            {
                strSql1.Append("Count,");
                strSql2.Append("'" + model.Count + "',");
            }
            strSql.Append("insert into T_PUB_PROJECT(");
            strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
            strSql.Append(")");
            strArry.Add(strSql);
            #endregion

            #region 添加项目材料表

            foreach (Model.Progect.T_PUB_PROJECTMATERIAL item in matralList)
            {
                StringBuilder strSqls = new StringBuilder();
                StringBuilder strSqls1 = new StringBuilder();
                StringBuilder strSqls2 = new StringBuilder();
                if (item.PROJECT_ID != null)
                {
                    strSqls1.Append("PROJECT_ID,");
                    strSqls2.Append("'" + item.PROJECT_ID + "',");
                }
                if (item.MATERIAL_ID != null)
                {
                    strSqls1.Append("MATERIAL_ID,");
                    strSqls2.Append("" + item.MATERIAL_ID + ",");
                }
                if (item.NUM != null)
                {
                    strSqls1.Append("NUM,");
                    strSqls2.Append("" + item.NUM + ",");
                }
                if (item.FLAG != null)
                {
                    strSqls1.Append("FLAG,");
                    strSqls2.Append("" + (item.FLAG ? 1 : 0) + ",");
                }
                if (item.REMARK != null)
                {
                    strSqls1.Append("REMARK,");
                    strSqls2.Append("'" + item.REMARK + "',");
                }

                strSqls.Append("insert into T_PUB_PROJECTMATERIAL(");
                strSqls.Append(strSqls1.ToString().Remove(strSqls1.Length - 1));
                strSqls.Append(")");
                strSqls.Append(" values (");
                strSqls.Append(strSqls2.ToString().Remove(strSqls2.Length - 1));
                strSqls.Append(")");
                strArry.Add(strSqls);
            }
            #endregion

            DBHelper.ExecuteSqlTran(strArry);
        }

        /// <summary>
        /// 项目编辑
        /// </summary>
        /// <param name="model"></param>
        /// <param name="matralList"></param>
        public void EditProgect(Model.Progect.T_PUB_PROJECT model, IList<Model.Progect.T_PUB_PROJECTMATERIAL> EditList, IList<Model.Progect.T_PUB_PROJECTMATERIAL> AddList)
        {
            ArrayList strArry = new ArrayList();

            #region 编辑项目表

            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_PUB_PROJECT set ");
            if (model.BRANCH_ID != null)
            {
                strSql.Append("BRANCH_ID=" + model.BRANCH_ID + ",");
            }
            else
            {
                strSql.Append("BRANCH_ID= null ,");
            }
            if (model.PROJECTTYPE_ID != null)
            {
                strSql.Append("PROJECTTYPE_ID=" + model.PROJECTTYPE_ID + ",");
            }
            else
            {
                strSql.Append("PROJECTTYPE_ID= null ,");
            }
            if (model.PROJECT_NAME != null)
            {
                strSql.Append("PROJECT_NAME='" + model.PROJECT_NAME + "',");
            }
            else
            {
                strSql.Append("PROJECT_NAME= null ,");
            }
            if (model.PRICE != null)
            {
                strSql.Append("PRICE=" + model.PRICE + ",");
            }
            else
            {
                strSql.Append("PRICE= null ,");
            }
            if (model.TIME != null)
            {
                strSql.Append("TIME='" + model.TIME + "',");
            }
            else
            {
                strSql.Append("TIME= null ,");
            }
            if (model.REMARK != null)
            {
                strSql.Append("REMARK='" + model.REMARK + "',");
            }
            else
            {
                strSql.Append("REMARK= null ,");
            }
            if (model.FLAG != null)
            {
                strSql.Append("FLAG='" + model.FLAG + "',");
            }
            else
            {
                strSql.Append("FLAG= null ,");
            }
            if (model.BUDGET != null)
            {
                strSql.Append("BUDGET='" + model.BUDGET + "',");
            }
            else
            {
                strSql.Append("BUDGET= null ,");
            }
            if (model.Count != null)
            {
                strSql.Append("Count='" + model.Count + "',");
            }
            else
            {
                strSql.Append("Count= null ,");
            }
            int n = strSql.ToString().LastIndexOf(",");
            strSql.Remove(n, 1);
            strSql.Append(" where PROJECT_ID='" + model.PROJECT_ID + "' ");
            strArry.Add(strSql);
            #endregion

            #region 编辑项目材料表
            foreach (Model.Progect.T_PUB_PROJECTMATERIAL item in EditList)
            {
                StringBuilder strSqls = new StringBuilder();
                strSqls.Append("update T_PUB_PROJECTMATERIAL set ");
                if (item.PROJECT_ID != null)
                {
                    strSqls.Append("PROJECT_ID='" + item.PROJECT_ID + "',");
                }
                else
                {
                    strSqls.Append("PROJECT_ID= null ,");
                }
                if (item.MATERIAL_ID != null)
                {
                    strSqls.Append("MATERIAL_ID=" + item.MATERIAL_ID + ",");
                }
                else
                {
                    strSqls.Append("MATERIAL_ID= null ,");
                }
                if (item.NUM != null)
                {
                    strSqls.Append("NUM=" + item.NUM + ",");
                }
                else
                {
                    strSqls.Append("NUM= null ,");
                }
                if (item.FLAG != null)
                {
                    strSqls.Append("FLAG=" + (item.FLAG ? 1 : 0) + ",");
                }
                else
                {
                    strSqls.Append("FLAG= null ,");
                }
                if (item.REMARK != null)
                {
                    strSqls.Append("REMARK='" + item.REMARK + "',");
                }
                else
                {
                    strSqls.Append("REMARK= null ,");
                }
                n = strSqls.ToString().LastIndexOf(",");
                strSqls.Remove(n, 1);
                strSqls.Append(" where ID=" + item.ID + "");
                strArry.Add(strSqls);
            }
            #endregion

            #region 添加项目材料表

            foreach (Model.Progect.T_PUB_PROJECTMATERIAL item in AddList)
            {
                StringBuilder strSqls1 = new StringBuilder();
                StringBuilder strSqls11 = new StringBuilder();
                StringBuilder strSqls21 = new StringBuilder();
                if (item.PROJECT_ID != null)
                {
                    strSqls11.Append("PROJECT_ID,");
                    strSqls21.Append("'" + item.PROJECT_ID + "',");
                }
                if (item.MATERIAL_ID != null)
                {
                    strSqls11.Append("MATERIAL_ID,");
                    strSqls21.Append("" + item.MATERIAL_ID + ",");
                }
                if (item.NUM != null)
                {
                    strSqls11.Append("NUM,");
                    strSqls21.Append("" + item.NUM + ",");
                }
                if (item.FLAG != null)
                {
                    strSqls11.Append("FLAG,");
                    strSqls21.Append("" + (item.FLAG ? 1 : 0) + ",");
                }
                if (item.REMARK != null)
                {
                    strSqls11.Append("REMARK,");
                    strSqls21.Append("'" + item.REMARK + "',");
                }

                strSqls1.Append("insert into T_PUB_PROJECTMATERIAL(");
                strSqls1.Append(strSqls11.ToString().Remove(strSqls11.Length - 1));
                strSqls1.Append(")");
                strSqls1.Append(" values (");
                strSqls1.Append(strSqls21.ToString().Remove(strSqls21.Length - 1));
                strSqls1.Append(")");
                strArry.Add(strSqls1);
            }
            #endregion

            DBHelper.ExecuteSqlTran(strArry);
        }

        #region 订单中获取项目
        public DataTable GetOrderProject(string strWhere)
        {
            StringBuilder varname1 = new StringBuilder();
            varname1.Append("SELECT p.PROJECT_NAME, \n");
            varname1.Append("       p.PROJECT_ID, \n");
            varname1.Append("       p.PRICE, \n");
            varname1.Append("       t.MATERIAL_ID,t.ID, \n");
            varname1.Append("       t.NUM, \n");
            varname1.Append("       m.MATERIAL_NAME, \n");
            varname1.Append("       m.IMAGEURL, \n");
            varname1.Append("       y.PROJECTTYPE_NAME, \n");
            varname1.Append("       mt.TYPE_NAME \n");
            varname1.Append("FROM   T_PUB_PROJECT p \n");
            varname1.Append("       LEFT JOIN T_PUB_PROJECTMATERIAL t \n");
            varname1.Append("         ON p.PROJECT_ID = t.PROJECT_ID \n");
            varname1.Append("       LEFT JOIN T_PUB_PROJECTTYPE y \n");
            varname1.Append("         ON y.PROJECTTYPE_ID = p.PROJECTTYPE_ID \n");
            varname1.Append("       LEFT JOIN T_PUB_MATERIAL m \n");
            varname1.Append("         ON m.MATERIAL_ID = t.MATERIAL_ID \n");
            varname1.Append("       LEFT JOIN dbo.T_PUB_MATERIALTYPE mt \n");
            varname1.Append("         ON mt.TYPE_ID = m.TYPE_ID ");
            varname1.Append("WHERE  1 > 0 \n");
            varname1.Append("       AND p.FLAG = 1 ");
            if (strWhere != "")
            {
                varname1.Append(strWhere);
            }

            Database db = DatabaseFactory.CreateDatabase();
            return db.ExecuteDataSet(CommandType.Text, varname1.ToString()).Tables[0];
        }
        #endregion

        public DataSet GetListWithMateral(string strWhere)
        {
            StringBuilder varname1 = new StringBuilder();
            varname1.Append("SELECT p.PROJECT_NAME, \n");
            varname1.Append("       p.PROJECT_ID, \n");
            varname1.Append("       p.PRICE, \n");
            varname1.Append("       t.MATERIAL_ID, \n");
            varname1.Append("       t.NUM, \n");
            varname1.Append("       b.BRANCH_NAME, \n");
            varname1.Append("       m.MATERIAL_NAME, \n");
            varname1.Append("       t.ID, \n");
            varname1.Append("       p.FLAG \n");
            varname1.Append("FROM   T_PUB_PROJECT p \n");
            varname1.Append("       LEFT JOIN T_PUB_PROJECTMATERIAL t \n");
            varname1.Append("         ON p.PROJECT_ID = t.PROJECT_ID \n");
            varname1.Append("       LEFT JOIN T_PUB_BRANCH b \n");
            varname1.Append("         ON p.BRANCH_ID = b.BRANCH_ID \n");
            varname1.Append("       LEFT JOIN T_PUB_MATERIAL m \n");
            varname1.Append("         ON m.MATERIAL_ID = t.MATERIAL_ID ");
            varname1.Append("  LEFT JOIN T_PUB_PROJECTTYPE y \n");
            varname1.Append("   on y.PROJECTTYPE_ID=p.PROJECTTYPE_ID \n");
            if (strWhere != "")
            {
                varname1.Append(" where 1>0" + strWhere);
            }
            varname1.Append(" order by p.Count desc");
            Database db = DatabaseFactory.CreateDatabase();
            return db.ExecuteDataSet(CommandType.Text, varname1.ToString());
        }
        public DataSet GetListWithMateralAndImage(string strWhere, string strWhereImage)
        {
            StringBuilder varname1 = new StringBuilder();
            varname1.Append("SELECT p.PROJECT_NAME, \n");
            varname1.Append("       p.PROJECT_ID, \n");
            varname1.Append("       p.PRICE, \n");
            varname1.Append("       t.MATERIAL_ID, \n");
            varname1.Append("       t.NUM, \n");
            varname1.Append("       b.BRANCH_NAME, \n");
            varname1.Append("       m.MATERIAL_NAME, \n");
            varname1.Append("       t.ID, \n");
            varname1.Append("       p.FLAG \n");
            varname1.Append("FROM   T_PUB_PROJECT p \n");
            varname1.Append("       LEFT JOIN T_PUB_PROJECTMATERIAL t \n");
            varname1.Append("         ON p.PROJECT_ID = t.PROJECT_ID \n");
            varname1.Append("       LEFT JOIN T_PUB_BRANCH b \n");
            varname1.Append("         ON p.BRANCH_ID = b.BRANCH_ID \n");
            varname1.Append("       LEFT JOIN T_PUB_MATERIAL m \n");
            varname1.Append("         ON m.MATERIAL_ID = t.MATERIAL_ID ");
            varname1.Append("  LEFT JOIN T_PUB_PROJECTTYPE y \n");
            varname1.Append("   on y.PROJECTTYPE_ID=p.PROJECTTYPE_ID \n");
            if (strWhere != "")
            {
                varname1.Append(" where 1>0" + strWhere);
            }
            varname1.Append(" order by p.Count desc ");
            string strGetImageSql = string.Format(@"select  T_PUB_FILE.URLThumbnail,T_PUB_FILE.URLVideoPhoto,T_PUB_FILE.TYPE ,T_PUB_PROJECT.PROJECT_ID
from  T_PUB_PROJECTFILE,T_PUB_FILE,T_PUB_PROJECT
where T_PUB_PROJECTFILE.FILE_ID=T_PUB_FILE.FILE_ID 
and T_PUB_PROJECT.PROJECT_ID=T_PUB_PROJECTFILE.PROJECT_ID {0}
order by T_PUB_FILE.TYPE", strWhereImage);
            varname1.Append(strGetImageSql);
            Database db = DatabaseFactory.CreateDatabase();
            return db.ExecuteDataSet(CommandType.Text, varname1.ToString());
        }
        /// <summary>
        /// 项目获得图片
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public DataTable GetProgectImage(string strWhere)
        {
            StringBuilder varname1 = new StringBuilder();
            varname1.Append("SELECT p.PROJECT_ID   AS id, \n");
            varname1.Append("       p.PRICE        AS price, \n");
            varname1.Append("       p.PROJECT_NAME AS name, \n");
            varname1.Append("       f.URLThumbnail, \n");
            varname1.Append("       f.URLOriginal ,f.URLVideoPhoto,f.FILE_ID fileID,f.TYPE as type,pt.PROJECTTYPE_NAME\n");
            varname1.Append("FROM   T_PUB_PROJECT p \n");
            varname1.Append("       LEFT JOIN T_PUB_PROJECTFILE pf \n");
            varname1.Append("         ON pf.PROJECT_ID = p.PROJECT_ID \n");
            varname1.Append("       LEFT JOIN T_PUB_FILE f \n");
            varname1.Append("         ON pf.FILE_ID = f.FILE_ID  left join dbo.T_PUB_PROJECTTYPE pt on p.PROJECTTYPE_ID=pt.PROJECTTYPE_ID\n");
            varname1.Append("WHERE  1>0 ");
            if (strWhere != "")
            {
                varname1.Append(strWhere);
            }
            varname1.Append(" order by  f.URLThumbnail desc, f.TYPE ");
            Database db = DatabaseFactory.CreateDatabase();
            return db.ExecuteDataSet(CommandType.Text, varname1.ToString()).Tables[0];
        }

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public List<Model.Progect.T_PUB_PROJECT> GetProjectList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select a.PROJECT_ID,BRANCH_ID,PROJECTTYPE_ID,PROJECT_NAME,PRICE,TIME,REMARK,FLAG,BUDGET ");
            strSql.Append(" FROM T_PUB_PROJECT as a, T_PUB_PROJECTFILE as b");
            strSql.Append(" where a.PROJECT_ID = b.FILE_ID ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(strWhere);
            }
            List<Model.Progect.T_PUB_PROJECT> list = new List<Model.Progect.T_PUB_PROJECT>();
            Database db = DatabaseFactory.CreateDatabase();
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
            }
            return list;
        }

        /// <summary>
        /// 智能提示项目名称
        /// </summary>
        /// <param name="branch_id"></param>
        /// <returns></returns>
        public DataSet GetProjectName(int branch_id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select DISTINCT PROJECT_NAME FROM T_PUB_PROJECT ");
            strSql.Append(" where BRANCH_ID=" + branch_id);
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        public DataSet GetProjectName()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select DISTINCT PROJECT_NAME FROM T_PUB_PROJECT ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public List<Model.Progect.T_PUB_PROJECT> GetListArrayName(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select DISTINCT PROJECT_NAME,PROJECT_ID,BRANCH_ID,PROJECTTYPE_ID,PRICE,TIME,REMARK,FLAG,BUDGET ");
            strSql.Append(" FROM T_PUB_PROJECT ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere + " group by PROJECT_NAME having count(*)=1");
            }
            List<Model.Progect.T_PUB_PROJECT> list = new List<Model.Progect.T_PUB_PROJECT>();
            Database db = DatabaseFactory.CreateDatabase();
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
            }
            return list;
        }

        #region  Method

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Model.Progect.T_PUB_PROJECT model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_PUB_PROJECT(");
            strSql.Append("PROJECT_ID,BRANCH_ID,PROJECTTYPE_ID,PROJECT_NAME,PRICE,TIME,REMARK,FLAG,BUDGET,Count)");

            strSql.Append(" values (");
            strSql.Append("@PROJECT_ID,@BRANCH_ID,@PROJECTTYPE_ID,@PROJECT_NAME,@PRICE,@TIME,@REMARK,@FLAG,@BUDGET,@Count)");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "PROJECT_ID", DbType.String, model.PROJECT_ID);
            db.AddInParameter(dbCommand, "BRANCH_ID", DbType.Int32, model.BRANCH_ID);
            db.AddInParameter(dbCommand, "PROJECTTYPE_ID", DbType.Int32, model.PROJECTTYPE_ID);
            db.AddInParameter(dbCommand, "PROJECT_NAME", DbType.String, model.PROJECT_NAME);
            db.AddInParameter(dbCommand, "PRICE", DbType.Int32, model.PRICE);
            db.AddInParameter(dbCommand, "TIME", DbType.DateTime, model.TIME);
            db.AddInParameter(dbCommand, "REMARK", DbType.String, model.REMARK);
            db.AddInParameter(dbCommand, "FLAG", DbType.Boolean, model.FLAG);
            db.AddInParameter(dbCommand, "BUDGET", DbType.Int32, model.BUDGET);
            db.AddInParameter(dbCommand, "Count", DbType.Int32, model.Count);
            int result;
            object obj = db.ExecuteScalar(dbCommand);
            if (!int.TryParse(obj.ToString(), out result))
            {
                return false;
            }
            return true;
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.Progect.T_PUB_PROJECT model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_PUB_PROJECT set ");
            strSql.Append("BRANCH_ID=@BRANCH_ID,");
            strSql.Append("PROJECTTYPE_ID=@PROJECTTYPE_ID,");
            strSql.Append("PROJECT_NAME=@PROJECT_NAME,");
            strSql.Append("PRICE=@PRICE,");
            strSql.Append("TIME=@TIME,");
            strSql.Append("REMARK=@REMARK,");
            strSql.Append("BUDGET=@BUDGET,");
            strSql.Append("FLAG=@FLAG,");
            strSql.Append("Count=@Count");
            strSql.Append(" where PROJECT_ID=@PROJECT_ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "PROJECT_ID", DbType.String, model.PROJECT_ID);
            db.AddInParameter(dbCommand, "BRANCH_ID", DbType.Int32, model.BRANCH_ID);
            db.AddInParameter(dbCommand, "PROJECTTYPE_ID", DbType.Int32, model.PROJECTTYPE_ID);
            db.AddInParameter(dbCommand, "PROJECT_NAME", DbType.String, model.PROJECT_NAME);
            db.AddInParameter(dbCommand, "PRICE", DbType.Int32, model.PRICE);
            db.AddInParameter(dbCommand, "TIME", DbType.DateTime, model.TIME);
            db.AddInParameter(dbCommand, "REMARK", DbType.String, model.REMARK);
            db.AddInParameter(dbCommand, "FLAG", DbType.Boolean, model.FLAG);
            db.AddInParameter(dbCommand, "BUDGET", DbType.Int32, model.BUDGET);
            db.AddInParameter(dbCommand, "Count", DbType.Int32, model.Count);
            int rows = db.ExecuteNonQuery(dbCommand);

            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string PROJECT_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_PUB_PROJECT ");
            strSql.Append(" where PROJECT_ID=@PROJECT_ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "PROJECT_ID", DbType.String, PROJECT_ID);
            int rows = db.ExecuteNonQuery(dbCommand);

            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.Progect.T_PUB_PROJECT GetModel(string PROJECT_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select PROJECT_ID,BRANCH_ID,PROJECTTYPE_ID,PROJECT_NAME,PRICE,TIME,REMARK,FLAG,BUDGET,Count from T_PUB_PROJECT ");
            strSql.Append(" where PROJECT_ID=@PROJECT_ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "PROJECT_ID", DbType.String, PROJECT_ID);
            Model.Progect.T_PUB_PROJECT model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select PROJECT_ID,BRANCH_ID,PROJECTTYPE_ID,PROJECT_NAME,PRICE,TIME,REMARK,FLAG,BUDGET,Count ");
            strSql.Append(" FROM T_PUB_PROJECT ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase();
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }
        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public List<Model.Progect.T_PUB_PROJECT> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select PROJECT_ID,BRANCH_ID,PROJECTTYPE_ID,PROJECT_NAME,PRICE,TIME,REMARK,FLAG,BUDGET,Count ");
            strSql.Append(" FROM T_PUB_PROJECT ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Model.Progect.T_PUB_PROJECT> list = new List<Model.Progect.T_PUB_PROJECT>();
            Database db = DatabaseFactory.CreateDatabase();
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public Model.Progect.T_PUB_PROJECT ReaderBind(IDataReader dataReader)
        {
            Model.Progect.T_PUB_PROJECT model = new Model.Progect.T_PUB_PROJECT();
            object ojb;
            model.PROJECT_ID = dataReader["PROJECT_ID"].ToString();
            ojb = dataReader["BRANCH_ID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.BRANCH_ID = (int)ojb;
            }
            ojb = dataReader["PROJECTTYPE_ID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.PROJECTTYPE_ID = (int)ojb;
            }
            model.PROJECT_NAME = dataReader["PROJECT_NAME"].ToString();
            ojb = dataReader["PRICE"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.PRICE = (int)ojb;
            }
            ojb = dataReader["TIME"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.TIME = (DateTime)ojb;
            }
            model.REMARK = dataReader["REMARK"].ToString();
            ojb = dataReader["FLAG"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.FLAG = (bool)ojb;
            }
            ojb = dataReader["BUDGET"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.BUDGET = Convert.ToInt32(ojb);
            }
            ojb = dataReader["Count"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.Count = Convert.ToInt32(ojb);
            }
            return model;
        }

        #endregion  Method

        #region 获得项目
        public DataTable GetProgect(string strWhere)
        {
            StringBuilder varname1 = new StringBuilder();
            varname1.Append("SELECT a.PROJECT_ID, \n");
            varname1.Append("       b.PROJECT_NAME \n");
            varname1.Append("FROM   T_PUB_PROJECTFILE a \n");
            varname1.Append("       LEFT JOIN T_PUB_PROJECT b \n");
            varname1.Append("         ON a.PROJECT_ID = b.PROJECT_ID ");
            if (strWhere != "")
            {
                varname1.Append(strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase();
            return db.ExecuteDataSet(CommandType.Text, varname1.ToString()).Tables[0];
        }
        #endregion

        #region  获得文件的主题婚礼
        public DataTable GetFileThem(string strFileID)
        {
            string strsql = string.Format(@"select T_PUB_WEDDINGTHEME.ID,T_PUB_WEDDINGTHEME.TITLE   from dbo.T_PUB_PROJECTFILE,dbo.T_PUB_WEDDINGTHEME
where T_PUB_PROJECTFILE.FILE_ID=T_PUB_WEDDINGTHEME.ID and T_PUB_PROJECTFILE.PROJECT_ID='{0}'", strFileID);
            Database db = DatabaseFactory.CreateDatabase();
            return db.ExecuteDataSet(CommandType.Text, strsql).Tables[0];
        }
        #endregion
    }
}
